package com.epfresh.supplier.activity.test_report;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.TestItemRecyclerViewAdapter;
import com.epfresh.supplier.adapter.TestReportExpandListAdapter;
import com.epfresh.supplier.adapter.TestReportRecyclerViewAdapter;
import com.epfresh.supplier.entity.TestItemEntity;
import com.epfresh.supplier.entity.TestReport;
import com.epfresh.supplier.entity.TestReportEntity;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestReportDetailActivity extends BaseActivity {
    private RecyclerView rv_test_item,rv_test_report;
    private List<TestItemEntity> testItemlist;
    private List<TestReportEntity> testReportList;
    private TestItemRecyclerViewAdapter testItemRecyclerViewAdapter;
    private TestReportRecyclerViewAdapter testReportRecyclerViewAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_report_detail);
        tvBack.setVisibility(View.VISIBLE);
        ivBack.setVisibility(View.VISIBLE);
        toolbarTitle.setText("检测详情");

        initView();
        initData();

        //加载数据
//        requestTestReportDetail();

    }



    private void initView() {
        rv_test_item=findViewById(R.id.rv_test_item);
        rv_test_report=findViewById(R.id.rv_test_report);
    }

    private void initData() {
        testItemlist=new ArrayList<>();
        testReportList=new ArrayList<>();
        final TestItemEntity testItemEntity=new TestItemEntity();
        testItemEntity.setTestItemName("农药残留");
        testItemEntity.setTechnologyRequirement("<50%");
        testItemEntity.setTestResult("<30%");
        testItemlist.add(testItemEntity);

        TestItemEntity testItemEntity2=new TestItemEntity();
        testItemEntity2.setTestItemName("DDT");
        testItemEntity2.setTechnologyRequirement("<5%");
        testItemEntity2.setTestResult("未检出");
        testItemlist.add(testItemEntity2);

        TestReportEntity testReportEntity=new TestReportEntity();
        testReportEntity.setTestReportUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1570686312365&di=c4324c361f0a7d383a57762b22bfbad7&imgtype=0&src=http%3A%2F%2Fr.1mutian.com%2Fuserfiles%2F006_.jpg");
        testReportList.add(testReportEntity);
        TestReportEntity testReportEntity2=new TestReportEntity();
        testReportEntity2.setTestReportUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1570687106776&di=aa9441ab9e074c642996a9fd7b62e825&imgtype=0&src=http%3A%2F%2Fimg05.hc360.cn%2F05%2Fcredit%2F136%2F800%2Fb%2F05-1368005.jpg");
        testReportList.add(testReportEntity2);
        TestReportEntity testReportEntity3=new TestReportEntity();
        testReportEntity3.setTestReportUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1570687106776&di=aa9441ab9e074c642996a9fd7b62e825&imgtype=0&src=http%3A%2F%2Fimg05.hc360.cn%2F05%2Fcredit%2F136%2F800%2Fb%2F05-1368005.jpg");
        testReportList.add(testReportEntity3);


        //创建adapter
        testItemRecyclerViewAdapter=new TestItemRecyclerViewAdapter(this,testItemlist);
        //绑定数据
        testItemRecyclerViewAdapter.setOnBindData(new TestItemRecyclerViewAdapter.BindData() {
            @Override
            public void onBind(TestItemRecyclerViewAdapter.ViewHolder viewHolder, TestItemEntity testItemEntity, int position) {
                viewHolder.tv_test_item_name.setText(testItemEntity.getTestItemName());//检查项目
                viewHolder.tv_technology_requirement.setText(testItemEntity.getTechnologyRequirement());//技术要求
                viewHolder.tv_test_result.setText(testItemEntity.getTestResult());//检查结果
            }
        });
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        rv_test_item.setLayoutManager(linearLayoutManager);
        rv_test_item.setAdapter(testItemRecyclerViewAdapter);


        testReportRecyclerViewAdapter=new TestReportRecyclerViewAdapter(this,testReportList);
        testReportRecyclerViewAdapter.setOnBindData(new TestReportRecyclerViewAdapter.BindData() {
            @Override
            public void onBind(TestReportRecyclerViewAdapter.ViewHolder viewHolder, TestReportEntity testReportEntity, int position) {
                ImageLoader.getInstance().displayImage(testReportEntity.getTestReportUrl(),viewHolder.iv_test_report);
            }
        });
        LinearLayoutManager linearLayoutManager1=new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        rv_test_report.setLayoutManager(linearLayoutManager1);
        rv_test_report.setAdapter(testReportRecyclerViewAdapter);
    }



    /**
     * 加载详情数据
     */
    private void requestTestReportDetail() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForDayOfMonth);
        Map<String, String> map = new HashMap<>();
        map.put("storeId", BaseApplication.getInstance().getStoreId());

        map.put("accountId",getUser().getAccountId());
        data.setParameters(map);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_order_incomeProductIncomeForDayOfMonth, onRequestListener);
    }

    private OnRequestListener<TestReport> onRequestListener = new OnRequestListener<TestReport>() {
        @Override
        public TestReport jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, TestReport.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            finish();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<TestReport> response, Object tag) {
            //隐藏加载动画
            hideProgressDialog();
            //获取数据
            TestReport responseElement = response.getResponseElement();
            //显示数据
            showInfo();

        }
    };

    private void showInfo() {

    }

}
